<?php

/**
 * ExtComboBoxBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
abstract class ExtComboBoxBase extends ExtTriggerField
{

    private $_staticitems_class_types = array('ExtComboBoxItem');
    private $is_static = false;
    private $is_dynamic = false;

    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);

        /* the first check is for custom store assignment (neither static nor liststore) */
        $this->is_dynamic = $this->getStore() != null;
        $this->parseStaticItems($node, $astwalker);
        $this->parseXTemplate('template', 'tpl', $node, $astwalker);
        $this->parseListSource($astwalker);
        $this->handleSetValue();
    }

    private function hasStoreOrListSource()
    {
        if ($this->is_static)
            return false;
        if ($this->is_dynamic)
            return true;
    }

    private function handleSetValue()
    {
        $value = trim($this->getValue());
        // if we are settings a value
        if ($value != "" && !is_null($value))
        {
            $value = $this->parseValue($value, null);

            // check the store or listsource (store)
            if ($this->hasStoreOrListSource())
                $this->getController()->addAfterInitScript(sprintf("ext4yii.setComboBoxValueWhenStore('%s',%s)", $this->getId(), $value));
            else
                $this->getController()->addAfterInitScript(sprintf("ext4yii.setComboBoxValueWhenStatic('%s',%s)", $this->getId(), $value));
        }
        else
        {
            /*
             * In this case the value is empty or null. We set it anyway. It will not have
             * any effect is ExtJS
             */
            if ($this->hasStoreOrListSource())
            {
                $this->getController()->addAfterInitScript(sprintf("ext4yii.setComboBoxValueWhenStore('%s')", $this->getId()));
            }
        }
    }

    private function parseListSource($astwalker)
    {
        $ls = $this->getListSource();
        if (!is_null($ls) && $ls != "")
        {
            $store = new ExtStore();
            $store->setAstWalker($astwalker);
            $store->setController($this->getController());
            $store->setId($this->getId() . '_store');
            $store->setUrl($this->getListSource());

            $this->setStore($this->JSImply($store->getId()));

            $fields = array();

            $text = new ExtReaderField();
            $text->setAstWalker($astwalker);
            $text->setController($this->getController());
            $text->setName($this->getDisplayField());
            $fields[] = $text->renderConfig(false);

            $data = new ExtReaderField();
            $data->setAstWalker($astwalker);
            $data->setController($this->getController());

            $data->setName($this->getValueField());
            $fields[] = $data->renderConfig(false);

            $json = new ExtJsonReader();
            $json->setArrayConfig('fields', $fields);
            $json->setRoot('rows');
            $json->setTotalProperty('counts');
            $json->setIdProperty($this->getValueField());

            $store->setConfig('autoLoad', true);
            $store->setJavaScriptConfig('reader', $json->renderConfig(true));

            $this->getController()->addDataStore($store);

            $this->mergeConfigs(array(
                'typeAhead' => true,
                'triggerAction' => 'all',
                'mode' => 'remote',
                'selectOnFocus' => true
            ));
            $this->is_dynamic = true;
        }
    }

    public function renderConfig($asobject=true)
    {
        $this->setHiddenName($this->getID());
        $this->setHiddenId($this->getID().'_');
        $this->setName($this->getID());
        $this->deleteConfig('listSource');
        $this->deleteConfig('value');
        return parent::renderConfig($asobject);
    }

    private function parseStaticItems($node, $astwalker)
    {
        $citems = $node->findPropertyByName('comboboxitems');
        if (!is_null($citems))
        {
            $items = parent::parseNodeWidthTag($citems,
                            $astwalker,
                            true,
                            null,
                            $this,
                            $this->_staticitems_class_types);

            if (count($items) != 0)
            {
                $data = implode(',', $items);
                $store = 'new Ext.data.ArrayStore({
                fields:["text","data"],
                data:[' . $data . ']
                })';

                $this->mergeConfigs(array(
                    'valueField' => 'data',
                    'displayField' => 'text',
                    'typeAhead' => 'true',
                    'triggerAction' => 'all',
                    'lazyRender' => 'true',
                    'mode' => 'local'
                ));

                $this->is_static = true;
                $this->setJavaScriptConfig('store', $store);
            }
        }
    }

}

?>
